package com.easydl.project.config;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;

/**
 * https://cloud.tencent.com/developer/article/1481440
 * META-INF spring.factories  EnvironmentPostProcessor
 * @author fxb208
 *
 */
public class CustomEnvironmentPostProcessor implements EnvironmentPostProcessor {
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
		Properties properties = new Properties();
		try {
			properties.load(new InputStreamReader(
					CustomEnvironmentPostProcessor.class.getClassLoader().getResourceAsStream("easydl.properties"),
					"UTF-8"));

			PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("easydl", properties);
			environment.getPropertySources().addLast(propertiesPropertySource);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
	}
}
